<template>
  <div class="app-container">
    <div class="top-container" style="display: flex;margin: 0 0 20px 0;">
      <div style="width: 100%;display: flex;justify-content: space-between;">
        <div style="width: 70%;">
          <el-input placeholder="请输入内容" v-model="input3" class="input-with-select" style="width: 80%;">
            <el-select v-model="select" slot="prepend" placeholder="请选择查询类型" style="width: 170px;">
              <el-option label="依据记录编号" value="0"></el-option>
              <el-option label="依据船只编号" value="1"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
          </el-input>
        </div>
        <div style="">
          <el-button type="success" @click="handleExport">导出Excel</el-button>
        </div>
      </div>
    </div>
    <div>
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column align="center" label="记录编号" width="105px" sortable>
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="捕捞者许可证" width="200" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.uid }}</span>
          </template>
        </el-table-column>
        <el-table-column class-name="status-col" label="船只编号" width="200" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.shipId | statusFilter">{{ scope.row.shipId }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="捕捞方式" width="160" align="center">
          <template slot-scope="scope">
            <span>{{ getFishingMethod(scope.row.way) }}</span>
          </template>
        </el-table-column>
        <el-table-column class-name="status-col" label="捕捞地点" width="160" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.locationX + '°N,' + scope.row.locationY + '°E' | statusFilter">{{ scope.row.locationX + '°N,' + scope.row.locationY + '°E'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="created_at" label="出海时间" width="200" sortable>
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span>{{ scope.row.beginTime }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="created_at" label="返回时间" width="200" sortable>
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span>{{ scope.row.endTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="成果汇报" width="100px">
          <template slot-scope="scope">
            <el-button type="text" @click="handleProduct(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="success" icon="el-icon-check" circle @click="showPassDialog(scope.row.id)"></el-button>
            <el-button size="small" type="danger" icon="el-icon-close" circle @click="showUnpassDialog(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 40px;">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage4"
        background :page-sizes="[10, 50, 100, 200]" :page-size="10" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <!--点击详情打开的对话框，不要放在table里，单独拿出来就行，不然有多少条记录就会渲染出多少个，非常卡-->
    <el-dialog title="捕捞成果明细(以斤为单位)" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="鱼类" :label-width="formLabelWidth">
          <el-input v-model="form.fish" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="虾蟹类" :label-width="formLabelWidth">
          <el-input v-model="form.sac" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="壳类" :label-width="formLabelWidth">
          <el-input v-model="form.shell" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="藻类" :label-width="formLabelWidth">
          <el-input v-model="form.alga" autocomplete="off" disabled ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <!--点击审核通过时打开的对话框，不要放在table里，单独拿出来就行，不然有多少条记录就会渲染出多少个，非常卡-->
    <el-dialog title="请确定你的操作" :visible.sync="dialogVisible1" width="30%" :before-close="handleClose">
      <span>您确定审核通过这条捕捞记录吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="success" @click="handlePass">确 定</el-button>
      </span>
    </el-dialog>
    <!--点击审核不通过时打开的对话框，不要放在table里，单独拿出来就行，不然有多少条记录就会渲染出多少个，非常卡-->
    <el-dialog title="请确定你的操作" :visible.sync="dialogVisible2" width="30%" :before-close="handleClose">
      <span>您确定要将这条捕捞记录审核为不合格吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="danger" @click="handleUnpass">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from '@/api/table'
//import {Edit, Delete,Search} from '@element-plus/icons-vue'
import { deleteRecord, getRecordList, searchFromRecord ,passRecord,getIdProduct} from '@/api/manager'
import { MessageBox } from 'element-ui'
import { A_exportRecord } from '@/api/excel'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      input3: '',
      select: '',
      dialogVisible1: false,
      dialogVisible2: false,
      dialogFormVisible: false,

      // 初始化表格数据信息
      pageSize:10,
      pageNum:0,
      status:0,
      // 数据总量
      total:0,
      // 通过/未通过id
      Id:null,

      form: {
        fish: '155',
        sac:'155',
        shell:'155',
        alga:'155',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.fetchData()
  },
  methods:
   {
    getFishingMethod(way) {
      const methodMap = {
        0: '拖网',
        1: '围网',
        2: '流刺网',
        3: '鱼笼',
        4: '其他',
      };
      return methodMap[way] || '未知';
    },

    handleProduct(id){
      this.dialogFormVisible = true
      let currentId=id
      getIdProduct(currentId).then(response=>{
        let productNum = response.data
        console.log(productNum)
        if(this.form.alga==null){
          this.form.alga='0'
        }
        else{
          this.form.alga=productNum.alga_total
        }
        if(this.form.fish==null){
          this.form.fish='0'
        }
        else{
          this.form.fish=productNum.fish_total
        }
        if(this.form.sac==null){
          this.form.sac='0'
        }
        else{
          this.form.sac=productNum.sac_total
        }
        if(this.form.shell==null){
          this.form.shell='0'
        }
        else{
          this.form.shell=productNum.shell_total
        }
      })
    },

    handleExport(){
      let s = this.select
      let k = this.input3
      if (this.select === ''|| this.select === null || this.select === undefined) {
          s = -1
      }
      if (this.input3 === ''|| this.input3 === null || this.input3 === undefined) {
          k = "null"
      }
      return A_exportRecord({
        keyword:k,
        searchType:parseInt(s),
        status:parseInt(0)}) // 0查看未审核通过列表
    },

    handleSizeChange(val) {
    console.log(`每页 ${val} 条`);
    this.pageSize = val; // 更新pageSize
    this.fetchData()  // 每次更改都重新请求 
    },

    handleCurrentChange(val) {
    console.log(`当前页: ${val}`);
    this.pageNum = val; // 更新pageNum
    this.fetchData()
    },

    fetchData() {
      
      console.log("Num=",this.pageNum)
      console.log("Size=",this.pageSize)
      this.listLoading = true
      getRecordList({
            pageNum:this.pageNum,
            pageSize:this.pageSize,
            status:0
      }).then(response=>{
        // 使用Object.entries()获取键值对数组
        const entries= Object.entries(response.data)
        console.log(entries)

        // 拿到total和num
        const [total,num]=entries[0]
        console.log("total=",total)
        console.log("num=",num)
        this.total=num
        
        //拿到list和数据
        const [key,value]=entries[1]
        console.log("key=",key)
        console.log("value=",value)

        this.list = value
        console.log("list=",this.list)
        this.listLoading = false
      })
    },
    // 处理查询
    handleSearch(){
      this.listLoading = true
      searchFromRecord({
        keyword:this.input3,
        pageNum:parseInt(this.pageNum),
        pageSize:parseInt(this.pageSize),
        searchType:parseInt(this.select),
        status:parseInt(0) // 0 查看审核未通过列表
      }).then(response=>{
        // 使用Object.entries()获取键值对数组
        const entries= Object.entries(response.data)
        console.log(entries)

        // 拿到total和num
        const [total,num]=entries[0]
        console.log("total=",total)
        console.log("value=",num)
        this.total=num
        
        //拿到list和数据
        const [key,value]=entries[1]
        console.log("key=",key)
        console.log("value=",value)

        this.list = value
        console.log("list=",this.list)
        this.listLoading = false
      })
    },

    // 展示通过审核弹窗
    showPassDialog(id){
      this.Id = id; // 设置通过审核的记录id
      this.dialogVisible1 = true; // 打开确认对话框
    },
    // 通过审核记录
    handlePass(){
      console.log("passId=",this.Id)
      passRecord(this.Id).then(response=>{
        MessageBox("操作成功")
        // 重新展示数据
        this.fetchData()
      })
      this.Id = null
      this.dialogVisible1 = false;
    },

    // 展示未通过审核弹窗
    showUnpassDialog(id) {
      this.Id = id; // 设置未通过审核的记录id
      this.dialogVisible2 = true; // 打开删除确认对话框
    },
    // 未通过审核记录
    handleUnpass(){
      console.log("unpassId=",this.Id)
      deleteRecord(this.Id).then(response=>{
        MessageBox("操作成功")
        // 重新展示数据
        this.fetchData()
      })
      this.Id = null
      this.dialogVisible2 = false;
    },
    
  
    test() {
      console.log(search_type)
      search_type = 'aaa'
      console.log(search_type)
    }
  }
}
</script>

<style>
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>
